<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * @param $url
 * @param null $postFields
 * @param string $method
 * @param string $refer
 * @param array $headers
 * @param int $timeout
 * @param int $connectTimeOut
 * @return array|mixed|null
 * 模拟提交数据函数
 */
function curlCall($url, $postFields = NULL, $method = "POST", $refer = '', $headers = array(), $timeout = 360, $connectTimeOut = 60) {
    $response = [];
    try {
        $ci = curl_init();
        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $connectTimeOut);
        curl_setopt($ci, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ci, CURLOPT_ENCODING, "");
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ci, CURLOPT_HEADER, FALSE);
        $https = substr($url, 0, 8) == "https://" ? TRUE : FALSE;
        if ($https) {
            curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        //伪造来源refer
        if (!empty($refer)) {
            curl_setopt($ci, CURLOPT_REFERER, $refer);
        }
        switch ($method) {
            case 'POST':
                curl_setopt($ci, CURLOPT_POST, TRUE);
                if (!empty($postFields)) {
                    curl_setopt($ci, CURLOPT_POSTFIELDS, $postFields);
                }
                break;
            case 'DELETE':
                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
                if (!empty($postFields)) {
                    $url = "{$url}?{$postFields}";
                }
                break;
            case 'GET':
                if (!empty($postFields)) {
                    $url = $url . "?" . http_build_query($postFields);
                }
        }
        curl_setopt($ci, CURLOPT_URL, $url);
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
        $response = curl_exec($ci);
        $info = curl_getinfo($ci, CURLINFO_HEADER_OUT);
        $httpCode = curl_getinfo($ci, CURLINFO_HTTP_CODE);
        \think\Log::write(__METHOD__ . "  url:[$url]   data[" . json_encode($postFields) . "] respone:[" . ($response) . "]  httpCode:[$httpCode]  headInfo:[" . json_encode($info) . "]", 'log');
        if ($httpCode != 200) {
            return null;
        }
        curl_close($ci);
    } catch (Exception $e) {
        \think\Log::write(__METHOD__ . "请求银行返回失败 ：" . json_encode($e), 'error');
    }
    return $response;
}